﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.TibcoMessage;
using System.Linq;

namespace HamcoWcs.Service.Srm.SrmTaskParsers
{
    /// <summary>堆垛机取货完成</summary>
    public class SrmParser_PickFinish : SrmParserBase
    {
        public override EnumSrmRequest RequestType => EnumSrmRequest.PickFinish;

        public override void Deal(SrmDto srm, SrmPlc srmPlc)
        {
            var taskEntity = GetSrmTask(srm);

            if (taskEntity == null) return;
          

            if (taskEntity.SubTaskType == EnumSubTaskType.SrmAbnormalInboundTask.ToString())
            {
                return;
            }

            taskEntity.MainTask.CurrentMachine = srm.MachineName;
            taskEntity.MainTask.CurrentPosition = srm.PortName;
            taskEntity.MainTask.CurrentType = "PORT";
            taskEntity.MainTask.CurrentZone= "";
            // 出库或移库时取货成功更改货架状态
            if (taskEntity.SubTaskType == EnumSubTaskType.SrmOutTask.ToString() ||
                taskEntity.SubTaskType == EnumSubTaskType.SrmMoveTask.ToString())
            {
                var loc = _wcsDbContext.WcsLocations.FirstOrDefault(x => x.WcsLocName == taskEntity.FromLoc);
                loc.LocStatus = (int)EnumLocStatus.Empty;
                loc.UnitId = "";
                loc.Remark = "";

                if (loc.ZoneName.Contains("Z6"))
                {
                    loc.LocStatus = (int)EnumLocStatus.HandOff;
                }

                _wcsDbContext.SaveChanges();
                var zoneData = _locationService.InventoryZonesByZone(loc.ZoneName);
                _mesCommunicateService.InventoryZoneDataRequest(new InventoryZoneDataReportMessage(new InventoryZoneDataReportMessageBody(loc.Aisle, zoneData)));
            }

            if (taskEntity.FromLoc.Contains("CJVCV100") || taskEntity.FromLoc.Contains("CJVCV200"))
            {
                var node = _wcsDbContext.NodeInfos.FirstOrDefault(x => x.NodeName == taskEntity.FromLoc);
                node.NodeMode = 0;
                _wcsDbContext.SaveChanges();
            }


            _logger.AddWcsRunLog(taskEntity.UnitId, $"{srm.MachineName}-{srm.PortName}取货完成");
            // srm取货完成上报MES位置变更信息
            _mesCommunicateService.PalletLocationChanged(new PalletLocationChangedMessage(new PalletLocationChangedMessageBody()
            {
                PalletName = srm.UnitId,
                TransportJobName = taskEntity.MainTask.TaskId,
                CurrentMachineName = srm.MachineName,
                CurrentPositionName = srm.PortName,
                CurrentPositionType = "PORT",
                TransferState = "MOVING",
                EventUser = srm.Aisle.NodeName,
                CurrentZoneName = "",
            }));
        }

        public SrmParser_PickFinish(ILocationService locationService, WcsDbContext wcsDbContext, IMesCommunicateService mesCommunicateService, ILoggerService logger)
        : base(wcsDbContext, locationService, mesCommunicateService, logger)
        {
        }
    }
}
